package algorithm.swordoff;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 从上到下打印二叉树
 * BFS使用队列
 */

public class SQ32_1 {

    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[0];

        LinkedList<Integer> list = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>() {{add(root);}};

        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            list.add(poll.val);
            if (poll.left != null) queue.add(poll.left);
            if (poll.right != null) queue.add(poll.right);
        }

        return list.stream().mapToInt(Integer::intValue).toArray();
    }

}
